<template>
	<view style="margin-left:10rpx;margin-right: 10rpx; height: 100%;">
		<view v-for="(item , index) in pages" :key="index">
			<!-- 轮播图 -->
			<Banner :image="item.data" v-if="item.type == 'banner'"></Banner>
			<!-- 三公图 -->
			<view v-if="item.type == 'blockMenu3'" class="m-t-b-sm">
				<BlockSan3 :list="item.data"></BlockSan3>
			</view>
			<!-- 店铺信息 -->
			<view v-if="item.type == 'shopItem'">
				<ShopItem :data="item.data"></ShopItem>
			</view>
			<!-- 卡片信息 -->
			<view v-if="item.type == 'infoCard'">
				<InfoCard></InfoCard>
			</view>
			<!-- 头部标题 -->
			<view v-if="item.type == 'titleTop'">
				<TitleTop :info="item.data"></TitleTop>
			</view>
			<!-- 产品直接购买 -->
			<view v-if="item.type == 'productToBuy'">
				<productToBuy :data="item" :onloadData="onloadData"></productToBuy>
			</view>
			<!-- 店铺信息 -->
			<view v-if="item.type == 'productBallList'">
				<productBallList :list="item.data"></productBallList>
			</view>

			<view v-if="item.type == 'grayInfo'">
				<InfoCard :data="item"></InfoCard>
			</view>
			<!-- 订单进行时上面 -->
			<view v-if="item.type == 'cardTop'">
				<cardTop :data="item"></cardTop>
			</view>
			<!-- 表单功能 -->
			<view v-if="item.type == 'form'">
				<forms :data="item" :onloadData="onloadData"></forms>
			</view>
			<!-- 列表 没分页功能 -->
			<view v-if="item.type == 'lists'">
				<uni-list>
					<uni-list-item v-for="(listInfo , listindex) in item.data" :key="listindex" :to="listInfo.to" link  :showArrow="listInfo.to" :title="listInfo.name" />
				</uni-list>
			</view>
			<!-- 订单列表 -->
			<view v-if="item.type == 'orderList'">
				<orderList :data="item"></orderList>
			</view>
			<!-- 跳转按钮 -->
			<view v-if="item.type == 'buttonLink' && item.data.show==true" class="m-t-b-sm">
				<navigator :url="item.data.url">
					<u-button size="large" :type='item.data.type'>{{item.data.name}}</u-button>
				</navigator>
			</view>
			<view class="flex flex-center" v-if="item.type == 'image'">
				<u-image :width="item.data.width?item.data.width:'450'" :height="item.data.height?item.data.height:'450'" :src="item.data.url"></u-image>
			</view>
		</view>
	</view>
</template>

<script>
	import Banner from "@/components/banner/banner.vue"
	import BlockSan3 from "@/components/menu/blockScan3.vue"
	import ShopItem from "@/components/layout/shopItem.vue"
	import InfoCard from "@/components/layout/infoCard.vue"
	import TitleTop from "@/components/layout/titleTop.vue"
	import list from "@/components/layout/list.vue"
	import orderList from "@/components/layout/orderListItem.vue"
	import listItem from "@/components/layout/listItem.vue"
	import productToBuy from "@/components/layout/productToBuy.vue"
	import productBallList from "@/components/layout/productBallList.vue"
	import fetch from '@/utils/http/fetch.js'
	import cardTop from "@/components/layout/cardTop.vue"
	import forms from "@/components/layout/form.vue"

	import {
		handleForm,
	} from '@/utils/http/http.js'

	export default {
		components: {
			Banner,
			BlockSan3,
			ShopItem,
			InfoCard,
			TitleTop,
			listItem,
			list,
			orderList,
			productToBuy,
			productBallList,
			cardTop,
			forms,
		},
		data() {
			return {
				firstLoading: false,
				loading: true,
				data: {},
				pages: [],
				onloadData: {},
				originData: {},
			}
		},

		async onShow() {
			
		},
		async onLoad(e) {
			this.onloadData = e
			// uni.setNavigationBarTitle({
			// 	title: '新的标题'
			// });
			// uni.setNavigationBarColor({
			//     frontColor: '#ffffff',
			//     backgroundColor: '#ff0000',
			//     animation: {
			//         duration: 400,
			//         timingFunc: 'easeIn'
			//     }
			// })
			await this.$onLaunched
			let publicConfig = uni.getStorageSync('publicConfig');

			if (!e.url) {
				e.url = 'Home/index'
			}
			if (!publicConfig.pages[e.url]) {
				uni.showToast({
					title: "缺少配置" + e.url,
				})
				return;
			}


			if (publicConfig.pages[e.url].async  || !publicConfig.pages[e.url]) {
				//需要异步请求
				let res = await fetch('/api/common/asyncData', 'GET', e)
				this.pages = res.data.pages;
				this.originData = res.data;
				uni.setNavigationBarTitle({
					title: res.data.title
				});
			} else {
				this.pages = publicConfig.pages[e.url].pages;
				uni.setNavigationBarTitle({
					title: publicConfig.pages[e.url].title
				});
				this.originData = publicConfig.pages[e.url];
			}
			if (this.originData.setTimeoutOnload) {
				this.onload(this.originData.setTimeoutOnload)
			}


			//异步获取
			for (var item of this.pages) {
				if (item.async) {
					item.url = e.url
					let params = {
						...item,
						...e
					}
					let res = await fetch('/api/common/asyncData', 'GET', params)
					item.data = res.data
				}
			}





		},
		methods: {
			scrolltolower() {
				console.log('触底')
			},
			onload(time) {
				setTimeout(function() {
					//重新加载
					const pages = getCurrentPages()
					// 声明一个pages使用getCurrentPages方法
					const curPage = pages[pages.length - 1]
					// 声明一个当前页面
					curPage.onLoad(curPage.options) // 传入参数
					curPage.onShow()
					curPage.onReady()
				}, time * 1000);

			}


		},
	}
</script>

<style>
	page {
		background-color: #f7f7f7
	}
</style>